from deepseek import *
import json

def get_index(ai : DeepSeek, keyword : str) -> json:
    prompt = '''
你是一个专业的知识库架构师，你最擅长帮忙解决知识库构建的问题。
下面你将通过用户的关键词生成一个知识库索引，并按照以下的json格式进行输出：
{
'name': '知识库名称',
'description': '知识库描述',
'list': [
{
'name': '一级目录名称',
'list': [
{
'name': '二级目录名称',
'list': [
{
'name': '知识点名称',
}
]
}
]
}
]
}
    '''
    message = Messages(prompt=prompt)
    ai.set_max_tokens(4096*2)
    ai.set_response_format('json')

    return ai.chat_with_json(message, keyword)
    
def write_index(index):
    with open('knowledge.md', 'w', encoding='utf-8') as f:
        f.write(f'# {index["name"]}\n')
        f.write(f'{index["description"]}\n')
        
        f.write('## 目录\n')
        for item in index['list']:
            f.write(f'- [{item["name"]}](#{item["name"]})\n')
            for sub_item in item['list']:
                f.write(f'  - [{sub_item["name"]}](#{sub_item["name"]})\n')
                for knowledge in sub_item['list']:
                    f.write(f'    - [{knowledge["name"]}](#{knowledge["name"]})\n')

        f.close()

def add_first_level_item(name):
    with open('knowledge.md', 'a+', encoding='utf-8') as f:
        f.write(f'\n## {name}\n')
        f.close()

def add_second_level_item(name):
    with open('knowledge.md', 'a+', encoding='utf-8') as f:
        f.write(f'\n### {name}\n')
        f.close()

def add_knowledge(ai, name, first_level_name, second_level_name, knowledge_name):
    with open('knowledge.md', 'a+', encoding='utf-8') as f:
        f.write(f'\n#### {knowledge_name}\n')
        text = get_knowledge(ai, name, first_level_name, second_level_name, knowledge_name)
        f.write(f'{text}\n')
        f.close()

def get_knowledge(ai : DeepSeek, name, first_level_name, second_level_name, knowledge_name):
    prompt = f'''
你是一个专业的知识库架构师，你最擅长帮忙解决知识库构建的问题。
下面请你详细解释以下的知识点：（输出时不要带有Markdown格式Letex格式）
'''
    message = Messages(prompt=prompt)
    
    return ai.chat(message, f'{name}的{first_level_name}的{second_level_name}的{knowledge_name}知识点')